<?php 
$css = ['/assets/jedate/skin/jedate.css'];
$this->load->view('admin/layout/head', array('title'=>"产品管理",'nav'=>'product','css'=>$css), FALSE);
switch ($this->session->admin_language) {
	case 'chinese':
		$prefix = '';
		break;
	case 'english':
		$prefix = 'en_';
		break;
	default:
		$prefix = '';
		break;
}

?>

<div id="container">
	<div class="toolbar">
		<?= form_open('', [
			'class' => 'form-inline',
			'id' => 'search-form',
			'method' => 'get',
		]) ?>
			<div class="form-group">
				<input type="text" name="begin_time"  class="form-control date-picker" value="<?= set_value('begin_time') ?>" placeholder="开始日期">
				<input type="text" name="end_time"  class="form-control date-picker" value="<?= set_value('end_time') ?>" placeholder="结束日期">
			</div>
			<div class="form-group">
				<select name="expire" id="search-expire-slt" class="form-control">
			  		<option value="">过期状态</option>
			  		<option value="1">已过期</option>
			  	</select>
			</div>
			<div class="form-group">
				<input type="text" name="keyword"  class="form-control" value="" placeholder="商品条形码">
			</div>
			<div class="form-group">
				<button class="btn btn-info">搜索</button>
			</div>
			<a href="<?= site_url('admin/purchaseRecord/create') ?>" class="btn btn-primary add-btn"><?= $this->lang->line('create'); ?></a>
		<?= form_close() ?>
	</div>
	
	<div class="table-container mt-10">
		<table class="table table-bordered table-striped table-hover scroll-table">
			<thead>
				<tr>
					<th>商品</th>
					<th>规格</th>
					<th>进货价格</th>
					<th>进货数量</th>
					<th>剩余数量</th>
					<th>进货时间</th>
					<th>过期时间</th>
					<th>备注</th>
					<th>操作</th>		
				</tr>
			</thead>
			<tbody>
				<?php foreach ($page->data() as $key => $value): ?>
					<tr>
						<td>
							<?php if($value['stock']>0 && $value['expire'] < time()): ?>
							<span style="color: #f30">[过期]</span>
							<?php endif; ?>
							<?= $value['product'][$prefix.'title'] ?>
						</td>
						<td><?= $value['item'][$prefix.'title'] ?></td>
						<td><?= $value['price'] ?></td>
						<td><?= $value['number']*1 ?></td>
						<td><?= $value['stock']*1 ?></td>
						<td><?= date('Y-m-d H:i', $value['create_time']) ?></td>
						<td><?= date('Y-m-d', $value['expire']) ?></td>
						<td><?= $value['remark'] ?></td>
						<td>
							<a href="javascript:;" class="modify-btn" pid="<?php echo $value['id'] ?>">修改</a>
							<?php if($value['stock']*1 == $value['number']*1): ?>
							<a href="javascript:;" class="del-btn" pid="<?php echo $value['id'] ?>">删除</a>
							<?php endif; ?>
						</td>
					</tr>
				<?php endforeach ?>
			</tbody>
			<tfoot>
				<?= $page->render() ?>
			</tfoot>
		</table>
	</div>
</div>

<div id="form-div" style="display: none">
	<form id="pr-form" class="form-horizontal">
	  <input type="hidden" name="id" value="0">
	  <div class="form-group row">	   
	    <label class="col-sm-3 control-label">进货价格：</label>
	    <div class="col-sm-6">
	    	<input type="text" name="price" value="" class="form-control" placeholder="" tautocomplete="off">
	    </div>
	  </div>
	  <div class="form-group row">	   
	    <label class="col-sm-3 control-label">进货数量：</label>
	    <div class="col-sm-6">
	    	<input type="text" name="number" value="" class="form-control" placeholder="">
	    </div>
	  </div>
	  <div class="form-group row">	   
	    <label class="col-sm-3 control-label">剩余数量：</label>
	    <div class="col-sm-6">
	    	<input type="text" name="stock" value="" class="form-control" placeholder="">
	    </div>
	  </div>
	  <div class="form-group row">	   
	    <label class="col-sm-3 control-label">总库存数：</label>
	    <div class="col-sm-6">
	    	<input type="text" name="item_stock" value="" class="form-control" placeholder="">
	    </div>
	  </div>
	  <div class="form-group row">	   
	    <label class="col-sm-3 control-label">过期日期：</label>
	    <div class="col-sm-6">
	    	<input type="text" name="expire" id="datetimepicker" value="" class="form-control" placeholder="">
	    </div>
	  </div>
	  <div class="form-group row">	   
	    <label class="col-sm-3 control-label">备注：</label>
	    <div class="col-sm-6">
	    	<input type="text" name="remark" value="" class="form-control" placeholder="">
	    </div>
	  </div>
	</form>
</div>


<?php 
$js = ['/assets/jedate/jedate.min.js'];
$this->load->view('admin/layout/footer',['js'=>$js]);
?>
<script>
$(function () {
	datepicker('.date-picker','<?php echo $this->session->admin_language ?>');

	$('input[name=keyword]').focus();
	var form_html = $('#form-div').html();
	$('#form-div').remove();

	//修改
	$('.modify-btn').on('click',function(){
		var tr = $(this).parents('tr');
		var id = $(this).attr('pid');
		var btn = '<a href="javascript:;" class="btn btn-primary user-form-submit">提交</a>';
		var dlg = dialog('修改记录',form_html,btn);
		
		$.getJSON('/admin/purchaseRecord/detail/'+id, function (json) {
			for(var k in json) {
				$('#pr-form input[name='+k+']').val(json[k]);
			}
		});

		$('.user-form-submit').on('click',form_submit);
		datepicker('#datetimepicker','<?php echo $this->session->admin_language ?>');
		
		// 日期控件绑定
		// $('#datetimepicker').datetimepicker({
		// 	format: 'YYYY-MM-DD',
		// 	autoclose: true,
		// 	startView: 1,
		// 	minView: 2,
		// 	language: lang
		// });
	})

	function form_submit(){
		var number = parseFloat($('#pr-form input[name=number]').val());
		var stock = parseFloat($('#pr-form input[name=stock]').val());
		var item_stock = parseFloat($('#pr-form input[name=item_stock]').val());

		if(number < stock){
			Toast.error('进货数量不能大于剩余数量');
			return;
		}

		if(item_stock < stock){
			Toast.error('总库存数量不能小于剩余数量');
			return;
		}



		$.post('/admin/purchaseRecord/modify', $('#pr-form').serialize(), function(json, textStatus, xhr) {
			if(json.code == 1){
				alert(json.msg);
				return;
			}
			Toast.success('操作成功',function(){
				window.location.reload();
			})
			
		});
	}

	//删除
	$('.del-btn').on('click',function(){
		var pid = $(this).attr('pid');
		confirm('确定要删除此记录吗？',function(e){
			if(e.index == 1){
				$.post('/admin/purchaseRecord/delete', {id: pid}, function(data, textStatus, xhr) {
					alert('删除成功！');
					window.location.reload();
				});
			}
		})
		
	})
})
</script>
